package com.yvon.maple.service.system.config;


import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executor;

@Configuration
@Component
public class NacosListener {

    @Autowired
    private NacosConfigManager nacosConfigManager;

    @Autowired
    private NacosConfigProperties nacosConfigProperties;

    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化nacos监听器");
        String dataId = "maple-service-system-dev.yaml";

        ConfigService configService = nacosConfigManager.getConfigService();
        configService.addListener(dataId, nacosConfigProperties.getGroup(), new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String s) {
                System.out.println("receive:" + s);
            }
        });
    }


    @NacosConfigListener(dataId = "maple-service-system-dev.yaml")
    public void onConfigChanged(String content) {
        System.out.println("onConfigChanged:" + content);
    }
}
